package com.company;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁的原理
 */
public class ReentrantLockTest {

    static final ReentrantLock  lock = new ReentrantLock( true );
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i  = 0 ;
                while ( true ){
                    test( "i"+i );
                    i++;
                    try {
                        Thread.sleep( 10 );
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread1.setName("j");
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                int j = 0;
                while (true) {
                    test("j"+j);
                    j++;
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread2.setName("j");
        thread2.start();
    }
    public static void test( String index ){
        lock.lock();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("=================="+index);
        //lock.unlock();
    }


}
